#include <Arduino.h>
#include "LEDMatrixDisplay.h"
#include "WiFiManager.h"
#include "TimeService.h"

// 硬件配置 - 32列8行LED矩阵
#define LED_PIN 8
#define LED_WIDTH 32
#define LED_HEIGHT 8

// 创建对象实例
LEDMatrixDisplay ledMatrix(LED_PIN, LED_WIDTH, LED_HEIGHT);
WiFiManager wifiManager("xiaomiWIFI", "hhz18394866900");
TimeService timeService;

// 显示状态管理
enum DisplayMode { TIME, DATE };
DisplayMode currentMode = TIME;
unsigned long lastModeChange = 0;
const unsigned long dateDisplayInterval = 10000; // 每10秒显示日期一次
const unsigned long dateDisplayDuration = 3000;  // 显示日期持续3秒

void setup() {
    Serial.begin(115200);
    delay(1000); // 等待串口连接
    Serial.println(F("桌面时钟启动中..."));
    
    // 初始化LED矩阵
    ledMatrix.begin();
    ledMatrix.setBrightness(100); // 中等亮度
    
    // 自定义布局
    DisplayLayout& layout = ledMatrix.getLayout();
    layout.setTimePosition(2, 0); // 时间显示位置 (居中)
    layout.setDatePosition(5, 1); // 从6改为5，为日期提供更多空间

    // 设置星期颜色
    ledMatrix.setWeekColors(
        CRGB(0, 255, 0),   // 当前星期的颜色 - 绿色
        CRGB(50, 50, 50)   // 其他星期的颜色 - 灰色
    );

    Serial.printf(F("LED矩阵尺寸: %d列 x %d行\n"), LED_WIDTH, LED_HEIGHT);
    Serial.printf(F("时间显示位置: (%d, %d)\n"), layout.getTimePositionX(), layout.getTimePositionY());
    Serial.printf(F("日期显示位置: (%d, %d)\n"), layout.getDatePositionX(), layout.getDatePositionY());
    Serial.printf(F("星期显示位置: y=%d\n"), layout.getWeekPositionY());
    
    // 设置颜色主题
    ledMatrix.setTimeColor(
        CRGB(0, 150, 255),   // 小时 - 亮蓝色
        CRGB(50, 255, 100),  // 分钟 - 亮绿色
        CRGB(255, 220, 0)    // 秒 - 亮金色
    );
    ledMatrix.setColonColor(CRGB(255, 80, 80)); // 冒号 - 亮红色
    ledMatrix.setDateColor(CRGB(200, 0, 255));  // 日期 - 亮紫色
    
    // 初始化WiFi和时间服务
    wifiManager.begin();
    timeService.begin();
    
    ledMatrix.fillScreen(CRGB::Red);
    delay(500);
    ledMatrix.clear();
    ledMatrix.testDrawDigit(0, 0, 8, CRGB::Green);
    delay(500);
    ledMatrix.displayTime(12, 34, 56, 1); // 测试星期1
    delay(1000);
    // 修正这里：使用 getDateColor() 而不是 setDateColor
    ledMatrix.drawDate(12, 25, 1, ledMatrix.getDateColor()); // 测试星期1
    delay(1000);
    ledMatrix.clear();
    
    Serial.println(F("桌面时钟已启动!"));
}

void loop() {
    static unsigned long lastLogTime = 0;
    wifiManager.handleClient();
    unsigned long currentMillis = millis();
    
    // 模式切换逻辑
    if (currentMode == TIME && 
        currentMillis - lastModeChange > dateDisplayInterval) {
        currentMode = DATE;
        lastModeChange = currentMillis;
        Serial.println(F("切换到日期模式"));
        ledMatrix.clear();
    } else if (currentMode == DATE && 
               currentMillis - lastModeChange > dateDisplayDuration) {
        currentMode = TIME;
        lastModeChange = currentMillis;
        Serial.println(F("切换到时间模式"));
        ledMatrix.clear();
    }

    if (wifiManager.isConnected()) {
        timeService.update();
        uint8_t dayOfWeek = timeService.getDayOfWeek();
        
        // 确保星期值在0-6范围内
        if (dayOfWeek > 6) {
            dayOfWeek = 0;
        }

        // 每秒输出一次日志
        if (currentMillis - lastLogTime >= 1000) {
            lastLogTime = currentMillis;
            TimeService::Date date = timeService.getDate();
            Serial.printf(F("时间: %02d:%02d:%02d, 日期: %04d-%02d-%02d, 星期: %d, 模式: %s\n"),
                          timeService.getHours(),
                          timeService.getMinutes(),
                          timeService.getSeconds(),
                          date.year, 
                          date.month, 
                          date.day,
                          dayOfWeek,
                          (currentMode == TIME) ? "时间" : "日期");
        }
        
        if (currentMode == DATE) {
            TimeService::Date date = timeService.getDate();
            ledMatrix.drawDate(date.month, date.day, dayOfWeek, ledMatrix.getDateColor());
        } else {
            ledMatrix.displayTime(
                timeService.getHours(),
                timeService.getMinutes(),
                timeService.getSeconds(),
                dayOfWeek
            );
        }
    } else {
        // 显示错误效果
        Serial.println(F("WiFi未连接! 显示错误效果"));
        ledMatrix.displayError();
    }
    
    delay(50);
}